/**
 * Copyright (c) [2025] [AGIROS] 
 * [travoddsgen] is licensed under Mulan PSL v2. 
 * You can use this software according to the terms and conditions of the Mulan PSL v2. 
 * You may obtain a copy of Mulan PSL v2 at: 
 *          http://license.coscl.org.cn/MulanPSL2 
 * THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND, 
 * EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT, 
 * MERCHANTABILITY OR FIT FOR A PARTICULAR PURPOSE. 
 * See the Mulan PSL v2 for more details.
 */ 

#include "CLangTypeSupportHeaderVisitor.h"

void CLangTypeSupportHeaderVisitor::visit(SpecificationNode& node) {
    // 从文件名中提取不带后缀的文件名
    std::string fileName = fileName_.substr(0, fileName_.find_last_of("."));
    // 文件头的宏声明
    std::string headerGuard = "#ifndef " + fileName + "_H\n";
    headerGuard += "#define " + fileName + "_H\n";
    content_ += headerGuard + "\n";

    // 头文件包含
    std::string includes = "";
    includes += "\n";
    content_ += includes;

    // __cplusplus宏定义开始
    content_ += "#ifdef __cplusplus\n";
    content_ += "extern \"C\" {\n";
    content_ += "#endif\n\n";

    // 递归访问子节点
    if (node.definitions_) {
        node.definitions_->accept(*this);
    }

    // __cplusplus宏定义结尾
    content_ += "#ifdef __cplusplus\n";
    content_ += "}\n";
    content_ += "#endif\n";

    // 文件尾的宏声明
    std::string endGuard = "#endif /* " + fileName + "_H */\n";
    content_ += endGuard;

    // 将_content写入文件
    writeToFile();
}

void CLangTypeSupportHeaderVisitor::visit(StructDefNode& node) {
    // 针对每个结构体XXX生成，XXXTypeSupport_get_instance函数
    std::string structName = typeNameVisitor_->findName(&node);
    content_ += "DDS_TYPE_API void* " + structName + "TypeSupport_get_instance();\n";
    content_ += "\n";
}

